package com.yuntian.baidu.util;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

@Slf4j
public class TreeUtil {


    public static void main(String[] args) {
        // 模拟您的JSON数据
        String deptStr = "[{\"id\":1,\"name\":\"总公司\",\"parent_id\":0,\"description\":\"集团总部，负责整体战略规划与管理\"},{\"id\":2,\"name\":\"技术部\",\"parent_id\":1,\"description\":\"负责公司所有技术研发与维护工作\"}," +
                "{\"id\":3,\"name\":\"前端开发组\",\"parent_id\":2,\"description\":\"专注于用户界面与前端交互开发\"},{\"id\":4,\"name\":\"Web前端小组\",\"parent_id\":3,\"description\":\"负责Web端页面开发与优化\"},{\"id\":5," +
                "\"name\":\"H5开发团队\",\"parent_id\":4,\"description\":\"专注于移动端H5页面开发\"},{\"id\":6,\"name\":\"后端开发组\",\"parent_id\":2,\"description\":\"负责服务器端逻辑与API开发\"},{\"id\":7," +
                "\"name\":\"Java开发小组\",\"parent_id\":6,\"description\":\"使用Java技术栈进行后端开发\"},{\"id\":8,\"name\":\"业务系统开发团队\",\"parent_id\":7,\"description\":\"负责公司核心业务系统开发\"},{\"id\":9," +
                "\"name\":\"财务部\",\"parent_id\":1,\"description\":\"负责公司财务核算与资金管理\"},{\"id\":10,\"name\":\"会计组\",\"parent_id\":9,\"description\":\"负责日常会计核算与报表编制\"},{\"id\":11,\"name\":\"成本核算小组\"," +
                "\"parent_id\":10,\"description\":\"专门负责公司成本核算与控制\"},{\"id\":12,\"name\":\"人力资源部\",\"parent_id\":1,\"description\":\"负责人力资源规划与员工管理\"}]";


        // 使用Hutool的TreeUtil直接构建树
        List<Tree<Integer>> treeList = cn.hutool.core.lang.tree.TreeUtil.build(JSONUtil.toList(deptStr, Map.class), 0, (node, tree) -> {
            tree.setId(((Number) node.get("id")).intValue());
            tree.setParentId(((Number) node.get("parent_id")).intValue());
            tree.setName((String) node.get("name"));
            tree.putExtra("description", node.get("description"));
        });
        // 转换为层级Map
        Map<Integer, List<Map<String, Object>>> levelMap = convertTreeToLevelMap(treeList);
        log.info("层级Map: {}", levelMap);

    }


    /**
     * 将Hutool的Tree结构转换为层级Map
     */
    public static Map<Integer, List<Map<String, Object>>> convertTreeToLevelMap(List<Tree<Integer>> treeList) {
        Map<Integer, List<Map<String, Object>>> levelMap = new LinkedHashMap<>();

        for (Tree<Integer> tree : treeList) {
            traverseHutoolTree(tree, 1, levelMap);
        }

        return levelMap;
    }

    /**
     * 递归遍历Hutool的Tree
     */
    private static void traverseHutoolTree(Tree<Integer> tree, int level,
                                           Map<Integer, List<Map<String, Object>>> levelMap) {
        // 获取或创建当前层级的列表
        List<Map<String, Object>> levelList = levelMap.computeIfAbsent(level, k -> new ArrayList<>());

        // 创建节点信息Map
        Map<String, Object> nodeInfo = new LinkedHashMap<>();
        nodeInfo.put("id", tree.getId());
        nodeInfo.put("name", tree.getName());
        nodeInfo.put("parent_id", tree.getParentId());
        //nodeInfo.put("description", tree.get("description"));
        nodeInfo.put("level", level);
        nodeInfo.put("extra",tree);
        levelList.add(nodeInfo);

        // 递归处理子节点
        if (tree.hasChild()) {
            for (Tree<Integer> child : tree.getChildren()) {
                traverseHutoolTree(child, level + 1, levelMap);
            }
        }
    }

    /**
     * 打印层级Map结果
     */
    private static void printLevelMap(Map<Integer, List<Map<String, Object>>> levelMap) {
        System.out.println("=== 树结构层级分布 ===");
        levelMap.forEach((level, nodes) -> {
            System.out.println("层级 " + level + ": " + nodes.size() + " 个节点");
            nodes.forEach(node -> System.out.println("  - " + node.get("name") + " (ID: " + node.get("id") + ")"));
        });
    }


}
